package com.sanhe.rfmuscle.activity.work;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.menu.app.event.work.TempEvent;
import com.sanhe.rfmuscle.App;
import com.sanhe.rfmuscle.R;
import com.sanhe.rfmuscle.databinding.ActivityTempBinding;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.event.Temp2Event;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TempActivity extends BaseWorkActivity {
    Typeface medium = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Medium.ttf");
    ActivityTempBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTempBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.title.setTypeface(medium);
        binding.ok.setTypeface(medium);

        binding.back.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x20));
        binding.up.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x57));
        binding.down.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x56));
        binding.ok.setOnClickListener(v -> sendTouchEvent((byte) 1, (byte) 0x44));

        binding.ok.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg_press);
                }else if (event.getAction() == MotionEvent.ACTION_UP) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg);
                }
                return false;
            }
        });
        updateTemp();
        updateTemp2();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TempEvent event) {
        updateTemp();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Temp2Event event) {
        updateTemp2();
    }

    private void updateTemp2() {
        binding.temp.setText(Event.temp2Event.getValue() + "");
    }

    private void updateTemp() {
        binding.tv.setText(Event.tempEvent.getValue() + "(℃)");
    }
}
